package cn.wolfcode.service.impl;

import cn.wolfcode.common.exception.BusinessException;
import cn.wolfcode.domain.AccountLog;
import cn.wolfcode.domain.AccountTransaction;
import cn.wolfcode.domain.OperateIntergralVo;
import cn.wolfcode.mapper.AccountLogMapper;
import cn.wolfcode.mapper.AccountTransactionMapper;
import cn.wolfcode.mapper.UsableIntegralMapper;
import cn.wolfcode.service.IUsableIntegralService;
import cn.wolfcode.web.msg.IntergralCodeMsg;
import com.alibaba.fastjson.JSON;
import io.seata.rm.tcc.api.BusinessActionContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;


@Service
@Slf4j
public class UsableIntegralServiceImpl implements IUsableIntegralService {
    @Autowired
    private UsableIntegralMapper usableIntegralMapper;
    @Autowired
    private AccountTransactionMapper accountTransactionMapper;
    @Autowired
    private AccountLogMapper accountLogMapper;

    @Override
    public void payIntergral(OperateIntergralVo operateIntergralVo) {
        //往account_log表中插入记录
        AccountLog accountLog = new AccountLog();
        accountLog.setAmount(operateIntergralVo.getValue());
        accountLog.setInfo(operateIntergralVo.getInfo());
        accountLog.setType(AccountLog.TYPE_DECR);
        accountLog.setGmtTime(new Date());
        accountLog.setPkValue(operateIntergralVo.getPk());
        //减少积分
        int count = usableIntegralMapper.decrIntergral(operateIntergralVo.getUserId(), operateIntergralVo.getValue());
        if (count == 0) {
            //积分余额不足
            throw new BusinessException(IntergralCodeMsg.INTERGRAL_NOT_ENOUGH);
        }
    }

    @Override
    public void refundIntergral(OperateIntergralVo operateIntergralVo) {
        //往account_log表中插入记录
        AccountLog accountLog = new AccountLog();
        accountLog.setAmount(operateIntergralVo.getValue());
        accountLog.setInfo(operateIntergralVo.getInfo());
        accountLog.setType(AccountLog.TYPE_INCR);
        accountLog.setGmtTime(new Date());
        accountLog.setPkValue(operateIntergralVo.getPk());
        //增加积分
        usableIntegralMapper.addIntergral(operateIntergralVo.getUserId(), operateIntergralVo.getValue());
    }

    @Override
    public void incrIntergralTry(OperateIntergralVo operateIntergralVo, BusinessActionContext context) {
        log.info("进入到了try逻辑中");
        common(operateIntergralVo, context, AccountTransaction.STATE_TRY, AccountTransaction.INCR_TYPE);
    }

    /*@Override
    public void incrIntergralTry(OperateIntergralVo operateIntergralVo, BusinessActionContext context) {
        log.info("进入到了try逻辑中");
        //1.往account_transaction表中插入一条记录,为了让cancel知道try之前有没有执行
        AccountTransaction accountTransaction = new AccountTransaction();
        accountTransaction.setActionId(String.valueOf(context.getBranchId())); //分支事务的id
        accountTransaction.setAmount(operateIntergralVo.getValue());
        accountTransaction.setGmtCreated(new Date());
        accountTransaction.setGmtModified(new Date());
        accountTransaction.setState(AccountTransaction.STATE_TRY);
        accountTransaction.setTxId(context.getXid());
        accountTransaction.setUserId(operateIntergralVo.getUserId());
        accountTransaction.setType(AccountTransaction.INCR_TYPE);
        accountTransactionMapper.insert(accountTransaction);
    }*/

    public void common(OperateIntergralVo operateIntergralVo, BusinessActionContext context, int state, int type) {
        AccountTransaction accountTransaction = new AccountTransaction();
        accountTransaction.setActionId(String.valueOf(context.getBranchId())); //分支事务的id
        accountTransaction.setAmount(operateIntergralVo.getValue());
        accountTransaction.setGmtCreated(new Date());
        accountTransaction.setGmtModified(new Date());
        accountTransaction.setState(state);
        accountTransaction.setTxId(context.getXid());
        accountTransaction.setUserId(operateIntergralVo.getUserId());
        accountTransaction.setType(type);
        accountTransactionMapper.insert(accountTransaction);
    }

    @Override
    public void incrIntergralCommit(BusinessActionContext context) {
        log.info("进入到了confirm逻辑中");
        //把数据从account_transaction表中查询出来
        AccountTransaction accountTransaction = accountTransactionMapper.get(context.getXid(), String.valueOf(context.getBranchId()));
        OperateIntergralVo operateIntergralVo = JSON.parseObject(context.getActionContext("operateIntergralVo").toString(), OperateIntergralVo.class);
        if (accountTransaction == null) {
            log.info("异常情况");
        }
        //判断状态是否为初始化状态,
        if (AccountTransaction.STATE_TRY.equals(accountTransaction.getState())) {
            //如果是初始化状态执行confirm业务逻辑
            usableIntegralMapper.addIntergral(operateIntergralVo.getUserId(), operateIntergralVo.getValue());
            //并且把状态改为confirm状态
            accountTransactionMapper.updateAccountTransactionState(context.getXid(),
                    String.valueOf(context.getBranchId()), AccountTransaction.STATE_COMMIT, AccountTransaction.STATE_TRY);
        } else if (AccountTransaction.STATE_COMMIT.equals(accountTransaction.getState())) {
            //如果状态为confirm状态,不做任何处理
        } else {
            log.info("异常情况");
        }
    }

    @Override
    public void incrIntergralRollback(BusinessActionContext context) {
        log.info("进入到了cancel逻辑中");
        //把数据从account_transaction表中查询出来
        AccountTransaction accountTransaction = accountTransactionMapper.get(context.getXid(), String.valueOf(context.getBranchId()));
        if (accountTransaction == null) {
            OperateIntergralVo operateIntergralVo = JSON.parseObject(context.getActionContext("operateIntergralVo").toString(), OperateIntergralVo.class);
            common(operateIntergralVo, context, AccountTransaction.STATE_CANCEL, AccountTransaction.INCR_TYPE);
        }
        if (AccountTransaction.STATE_TRY.equals(accountTransaction.getState())) {
            //把状态修改为cancel状态
            accountTransactionMapper.updateAccountTransactionState(context.getXid(),
                    String.valueOf(context.getBranchId()), AccountTransaction.STATE_CANCEL, AccountTransaction.STATE_TRY);
        } else if (AccountTransaction.STATE_CANCEL.equals(accountTransaction.getState())) {
            //如果状态为cancel状态,不做任何处理
        } else {
            log.info("异常情况");
        }
    }
}
